import pandas as pd
import numpy as np

# 创建 Series 和 DataFrame 示例
data_series = [10, 20, 30, 40]
s = pd.Series(data_series, index=['a', 'b', 'c', 'd'])
print("创建的 Series:")
print(s)
data_dict = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35], 'City': ['New York', 'Los Angeles', 'Chicago']}
df = pd.DataFrame(data_dict)
print("\n创建的 DataFrame:")
print(df)
print('===========================创建示例=========================')
# 添加数据
s.loc['e'] = 50  # Series：通过新索引添加元素
df['Country'] = ['USA', 'USA', 'USA']  # DataFrame：添加新列
df.loc[2, 'City'] = 'San Francisco'
print("\n添加数据:")
print("Series 添加新数据:")
print(s)
print("DataFrame 添加新列:")
print(df)
print('===========================添加示例=========================')
# 填充缺失值
s_with_na = s.copy() # 创建副本
s_with_na['b'] = np.nan # 添加缺失值Nan
s_filled = s_with_na.fillna(99)  # Series：填充缺失值
df_with_na = df.copy()
df_with_na.loc[1, 'Age'] = pd.NA
df_with_na.loc[1, 'City'] = np.nan
df_filled = df_with_na.fillna({'Age': 40, 'City': 'Unknown'})  # DataFrame：填充缺失值
print("\n填充缺失值:")
print("Series 填充缺失值:")
print(s_filled)
print("DataFrame 填充缺失值:")
print(df_filled)
print('===========================填充示例=========================')

# 删除重复数据
s_duplicate = pd.Series([10, 20, 20, 40, 40, 40])
df_duplicate = pd.DataFrame({'Name': ['Alice', 'Bob', 'Bob', 'Charlie', 'Charlie', 'Charlie'], 'Age': [25, 30, 30, 35, 35, 35]})
s_no_duplicates = s_duplicate.drop_duplicates()  # Series：删除重复值
df_no_duplicates = df_duplicate.drop_duplicates()  # DataFrame：删除重复值
print("\n删除重复数据:")
print("Series 删除重复值:")
print(s_no_duplicates)
print("DataFrame 删除重复行:")
print(df_no_duplicates)
print('===========================删除重复示例=========================')
# 删除缺失值
s_with_na_2 = s.copy()
s_with_na_2['c'] = np.nan
s_without_na = s_with_na_2.dropna()  # Series：删除缺失值
df_with_na_2 = df.copy()
df_with_na_2.loc[0, 'Age'] = np.nan
df_without_na = df_with_na_2.dropna()  # DataFrame：删除包含缺失值的行
print("\n删除缺失值:")
print("Series 删除缺失值:")
print(s_without_na)
print("DataFrame 删除缺失值:")
print(df_without_na)
print('===========================删除缺失示例=========================')
# 修改数据
s['a'] = 100  # Series：修改单个元素
df['Age'] = [26, 31, 36]  # DataFrame：修改指定列的值
print("\n修改数据:")
print("Series 修改数据:")
print(s)
print("DataFrame 修改数据:")
print(df)
print('===========================修改示例=========================')
# 合并数据
s2 = pd.Series([50, 60, 70], index=['e', 'f', 'g'])
s_merged = pd.concat([s, s2], axis=0)  # Series：合并
df2 = pd.DataFrame({'Name': ['David', 'Eva'], 'Age': [40, 45], 'City': ['Seattle', 'Austin']})
df_merged = pd.concat([df, df2], axis=0)  # DataFrame：按行合并
df_merged_columns = pd.concat([df, df2], axis=1)  # DataFrame：按列合并
print("\n合并数据:")
print("Series 合并:")
print(s_merged)
print("DataFrame 按行合并:")
print(df_merged)
print("DataFrame 按列合并:")
print(df_merged_columns)
print('===========================合并示例=========================')
# 查看前几项
print("\n查看前几项:")
print("Series 前 3 项:")
print(s.head(3))  # 查看前 3 项
print("DataFrame 前 2 行:")
print(df.head(2))  # 查看前 2 行
print('===========================查看前项示例=========================')
# 查看后几项
print("\n查看后几项:")
print("Series 后 2 项:")
print(s.tail(2))  # 查看后 2 项
print("DataFrame 后 1 行:")
print(df.tail(1))  # 查看后 1 行
print('===========================查看后项示例=========================')
# 查看数据
print("\n查看数据:")
print("Series 的值:")
print(s.values)  # 获取 Series 的值
print("DataFrame 的值:")
print(df.values)  # 获取 DataFrame 的值
print('===========================查看数据示例=========================')
# 查看索引
print("\n查看索引:")
print("Series 的索引:")
print(s.index)  # 获取 Series 的索引
print("DataFrame 的索引:")
print(df.index)  # 获取 DataFrame 的索引
print('===========================查看索引示例=========================')
# 按位置选择数据
print("\n按位置选择数据:")
print("Series 按位置选择（第 2 个元素）:")
print(s.iloc[1])  # 根据位置选择
print("DataFrame 按位置选择（第 2 行）:")
print(df.iloc[1])  # 根据位置选择
print('==========================查看位置示例=========================')
# 按标签选择数据
print("\n按标签选择数据:")
print("Series 按标签选择（索引 'a' 的元素）:")
print(s.loc['a'])  # 根据标签选择
print("DataFrame 按标签选择（行标签为 'Bob'）:")
print(df.loc[1])  # 根据行标签选择
print("DataFrame 按标签选择（列 'City' 的数据）:")
print(df['City'])  # 根据列标签选择
print('==========================查看标签示例=========================')
# 按条件选择数据
print("\n按条件选择数据:")
print("Series 按条件选择（大于 20 的元素）:")
print(s[s > 20])  # 条件筛选
print("DataFrame 按条件选择（Age 大于 30 的行）:")
print(df[df['Age'] > 30])  # 条件筛选
print('==========================查看条件示例=========================')